package com.yx.doodleview.ui.widget.doodleveiw;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.yx.doodleview.ui.widget.doodleveiw.action.BaseAction;
import com.yx.doodleview.ui.widget.doodleveiw.action.BitmapAction;
import com.yx.doodleview.ui.widget.doodleveiw.action.PathAction;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yx on 2017/8/18.
 */

public class EditDoodleView extends SurfaceView implements SurfaceHolder.Callback {

    private SurfaceHolder mSurfaceHolder;
    private Canvas mCanvas;
    private List<BaseAction> baseActions;
    private BaseAction curAction;// 当前绘制图形
    private int curColor = Color.BLACK;
    private int curWidth = 1;
    private Bitmap mBitmap;
    private Paint mPaint;
    /**
     * 画板背景
     **/
    private Bitmap backgroundBitmap = null;
    /**
     * 绘画图层
     **/
    private Bitmap paintBitmap = null;

    public EditDoodleView(Context context) {
        this(context, null);
    }

    public EditDoodleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EditDoodleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    private void initView() {
        mSurfaceHolder = getHolder();
        //注册回调
        mSurfaceHolder.addCallback(this);
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(curWidth);
//        setFocusable(true);
//        setKeepScreenOn(true);
//        setFocusableInTouchMode(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        //创建
        baseActions = new ArrayList<>();
        Log.e("surfaceCreated", "surfaceCreated");
        //获得canvas对象
        mCanvas = mSurfaceHolder.lockCanvas();
        //绘制背景
        mCanvas.drawColor(Color.WHITE);
        if (backgroundBitmap != null && !backgroundBitmap.isRecycled()) {
            BitmapAction bitmapAction = new BitmapAction(backgroundBitmap, Color.WHITE);
            bitmapAction.draw(mCanvas);
//            mCanvas.drawBitmap(backgroundBitmap, 0, 0, null);
            baseActions.add(bitmapAction);
        }
        mSurfaceHolder.unlockCanvasAndPost(mCanvas);
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        //改变
        Log.e("surfaceChanged", "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        //销毁
        Log.e("surfaceDestroyed", "surfaceDestroyed");
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float touchX = event.getX();
        float touchY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                curAction = new PathAction(touchX, touchY, curWidth, curColor);
                break;
            case MotionEvent.ACTION_MOVE:
                mCanvas = mSurfaceHolder.lockCanvas();
                mCanvas.drawColor(Color.WHITE);
                for (BaseAction action : baseActions) {
                    action.draw(mCanvas);
                }
                curAction.move(touchX, touchY);
                curAction.draw(mCanvas);
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
                break;
            case MotionEvent.ACTION_UP:
                baseActions.add(curAction);
                curAction = null;
                break;
            default:
                break;

        }

        return true;

    }

    /**
     * 设置绘制的背景图片 创建绘画画板Canvas
     *
     * @param bitmap 背景图片
     */
    public void setBackgroundBitmap(Bitmap bitmap) {
        backgroundBitmap = bitmap;
        paintBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(), Bitmap.Config.ARGB_8888);

    }


    /**
     * 设置绘制颜色
     *
     * @param color
     */
    public void setColor(int color) {
        curColor = color;
    }

    /**
     * 设置画笔粗细
     *
     * @param stokenSize
     */
    public void setStokenWidth(int stokenSize) {
        curWidth = stokenSize;
    }


    /**
     * 重新绘制
     */
    public void reset() {
        if (baseActions != null && baseActions.size() > 0) {
            baseActions.clear();
            Canvas mCanvas = mSurfaceHolder.lockCanvas();
            mCanvas.drawColor(Color.WHITE);
            mSurfaceHolder.unlockCanvasAndPost(mCanvas);
            curAction = null;
        }

    }


    /**
     * 涂鸦后的bitmap
     *
     * @return
     */
    public Bitmap getBitmap() {
        mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(mBitmap);
        canvas.drawBitmap(backgroundBitmap, 0, 0, null);
        doDraw(canvas);
        return mBitmap;
    }

    /**
     * 获取绘制后的bitmap
     *
     * @param canvas
     */
    private void doDraw(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);
        for (BaseAction action : baseActions) {
            action.draw(canvas);
        }
        canvas.drawBitmap(mBitmap, 0, 0, mPaint);
    }


}
